package day22.HomeworkCollections;

import day21.Collections.Vegetable;

public class Product {

	private static final double MIN_PRICE = 0.0;
	private static final double MAX_PRICE = 10000.0;
	private static final double DEF_PRICE = 0.0;
	private static final String DEF_MEASUREMENT_UNIT = "Pieces";
	private static final int DEFAULT_QUANTITY = 0;
	private static final int MAX_QUANTITY = 10000;
	private static final int MIN_QUANTITY = 0;
	private static final String DEFAULT_PRODUCT_NAME = "Default Product Name";
	private String name;
	private double quantity;
	private String measurementUnit;
	private double perUnitPrice;

	public Product() {

	}

	public Product(String name) {
		this();

		if (name != null && !name.equals("")) {
			this.name = name;
		} else {
			this.name = DEFAULT_PRODUCT_NAME;
		}
	}

	public Product(String name, double quantity, String measurementUnit, double perUnitPrice) {
		this(name);

		if (MIN_QUANTITY <= quantity && quantity <= MAX_QUANTITY) {
			this.quantity = quantity;
		} else {
			this.quantity = DEFAULT_QUANTITY;
		}

		if (measurementUnit != null && !measurementUnit.equals("")) {
			this.measurementUnit = measurementUnit;
		} else {
			this.measurementUnit = DEF_MEASUREMENT_UNIT;
		}
		
		if (MIN_PRICE <= perUnitPrice && perUnitPrice <= MAX_PRICE){
			this.perUnitPrice = perUnitPrice;
		} else {
			this.perUnitPrice = DEF_PRICE;
		}
		
	}

	public String getName() {
		return name;
	}

	public double getQuantity() {
		return quantity;
	}

	public String getMeasurementUnit() {
		return measurementUnit;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj != null && obj instanceof Product)
			if (((Product) obj).name != null)
				return ((Product) obj).name.equals(this.name);
		return false;
	}

	@Override
	public String toString() {
		
		String objectToString = "";

		if (this.name != null) {
			objectToString += this.name;
		}

		objectToString += ", " + this.quantity;

		if (this.measurementUnit != null) {
			objectToString += " " + this.measurementUnit;
		}
		
		objectToString +=", total price: " + this.perUnitPrice*this.quantity;

		return objectToString;
	}

}
